//
//  ObjectSlicing.hpp
//  learnC++
//
//  Created by liuzhibing on 2018/6/17.
//  Copyright © 2018年 yunfan.com. All rights reserved.
//

#ifndef ObjectSlicing_hpp
#define ObjectSlicing_hpp

#include <stdio.h>
#include <iostream>
#include <string>
//对象切片

class Base11 {
    int m_value;
    std::string m_name;
    
public:
    
    Base11(std::string name = "",int value = 0):m_name(name),m_value(value){
        
    }
    
    virtual const std::string getName() const {return "Base";}
    int getValue(){return m_value;}
};

class Derived11 : public Base11 {
    
    
public:
    
    Derived11(std::string name = "",int value = 0):Base11(name,value){
        
    }
    
    virtual const std::string getName() const {return "Derived";}
    
};

static void printName(const Base11 /*正确写法添加&*/base){ // note: base passed by value, not reference
    std::cout << "I am a " << base.getName() << '\n';
}

/*
 Derived11 d("alex",18);
 
 Base11 pb = d;//对象裁剪，会将d的基类部分完全赋值，派生类部分裁减掉
 //Base11 &pb = d; 这样就还是原地址了
 std::cout << pb.getName() << pb.getValue();
 
 
 NOTE:
 Derived11 d("alex",18);
 printName(d);//因为这里是值传递，所以会相当于创建一个Base = d，相当于裁剪赋值所以结果是打印i am base,所以类作为参数时，总是传递引用或者指针是有用的（可以避免对象裁剪）
 
 
 */


#endif /* ObjectSlicing_hpp */

